package com.gfxy.common.portal.service;

import com.gfxy.common.portal.dao.CommonMapper;
import com.gfxy.common.portal.dao.SysLoginUserMapper;
import com.gfxy.common.portal.entity.SysLoginUser;
import com.gfxy.common.portal.entity.SysLoginUserExample;
import com.gfxy.commons.crypto.DESCoder;
import com.gfxy.commons.utils.UUIDUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

/**
 * @author: lijixin 2019-05-17
 */
@Service
public class UserService {

    private static final Logger logger = LoggerFactory.getLogger(UserService.class);

    @Autowired
    private SysLoginUserMapper userMapper;

    @Autowired
    private CommonMapper commonMapper;

    public List<SysLoginUser> findUserByLoginName(String username) {
        SysLoginUserExample example = new SysLoginUserExample();
        example.createCriteria().andLoginNameEqualTo(username);
        List<SysLoginUser> list = userMapper.selectByExample(example);
        return list;
    }

    public List<Map> findUserByDeptId(String deptId){
        logger.info("查询部门Id为：" + deptId  + "的所用用户");
        List<Map> result = commonMapper.selectAllUserInfoByOrganizationId(deptId);

        logger.info("部门Id为：" + deptId  + "的用户共有" + result.size() +"个 ：" + result);
        return result;
    }

    public int addUser(SysLoginUser user) throws NoSuchAlgorithmException {
        user.setUserId(UUIDUtil.getUUID());
        user.setPassword(DESCoder.encryptMD5("88888888"));

        return userMapper.insert(user);
    }

    public int updateUser(SysLoginUser user){
        return userMapper.updateByPrimaryKeySelective(user);
    }

    public void deleteUser(String userId) {
        //todo 删除关联角色
        userMapper.deleteByPrimaryKey(userId);

    }


    /**
     * 重置密码
     * @param userId
     */
    public void reSetUserPwd(String userId) throws NoSuchAlgorithmException {
        SysLoginUser user = new SysLoginUser();
        user.setUserId(userId);
        user.setPassword(DESCoder.encryptMD5("88888888"));
        userMapper.updateByPrimaryKeySelective(user);
    }
}
